//
//  XMTool.m
//  单例模式
//
//  Created by 李煜 on 2024/3/21.
//

#import "XMTool.h"

@implementation XMTool
#pragma mark - public
+ (instancetype)shareTool {
   return [[self alloc]init];
}
#pragma mark - private
static XMTool *_instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
   //加互斥锁解决多线程访问安全问题
//   @synchronized (self) {
//      if (_instance == nil) {
//         _instance = [super allocWithZone:zone];
//      }
//   }

   ///第二种方法 使用GCD解决多线程安全问题,gcd本身就是线程安全的
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
      _instance = [super allocWithZone:zone];
   });
   return _instance;
}

-(id)copyWithZone:(NSZone *)zone {
   return  _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone {
   return _instance;
}
#if __has_feature(objc_arc)

#else
-(oneway void)release {

}
- (instancetype)retain {
   return _instance;
}
- (NSUInteger)retainCount {
   return MAXFLOAT;
}
#endif

@end
